package com.jymj.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by dl on 2017/8/3.
 */
public class TimeString {

    private final Logger LOGGER = LoggerFactory.getLogger(TimeString.class);

    private String valueOfString(String str, int len) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len - str.length(); i++) {
            sb.append("0");
        }
        return (sb.length() == 0) ? (str) : (sb.toString() + str);
    }

    public String getDateFormat() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }

    public Date getDateFormat(String time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return df.parse(time);
        } catch (ParseException e) {
            LOGGER.error(e.getMessage());
        }
        return null;
    }

    private String getTimeString(Calendar calendar) {
        StringBuilder sb = new StringBuilder();
        sb.append(String.valueOf(calendar.get(Calendar.YEAR)))
                .append(this.valueOfString(String.valueOf(calendar.get(Calendar.MONTH) + 1), 2))
                .append(this.valueOfString(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)), 2))
                .append(this.valueOfString(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)), 2))
                .append(this.valueOfString(String.valueOf(calendar.get(Calendar.MINUTE)), 2))
                .append(this.valueOfString(String.valueOf(calendar.get(Calendar.SECOND)), 2))
                .append(this.valueOfString(String.valueOf(calendar.get(Calendar.MILLISECOND)), 3));
        return sb.toString();
    }

    public String getTimeString(String time) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(this.getDateFormat(time));
        return this.getTimeString(calendar);
    }

    public String getTimeString() {
        Calendar calendar = new GregorianCalendar();
        return this.getTimeString(calendar);
    }
}
